package com.duruikang.LeaveAPI.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duruikang.LeaveAPI.dao.AccountDao;
import com.duruikang.LeaveAPI.entity.Account;
import com.duruikang.LeaveAPI.service.LoginService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Drik
 * @date 2021-12-09 18:16
 */
@Service
public class LoginServiceImpl extends ServiceImpl<AccountDao, Account> implements LoginService {

    @Override
    public Map<String, String> login(String userName, String password) {
        Map<String, String> map = new HashMap<>();
        int res = query().eq("username", userName).count();
        if (res == 0) {
            // 用户名不存在
            map.put("res", "-1");
            return map;
        }
        Account account = query().eq("username", userName)
                .one();
        if (password.equals(account.getPassword())) {
            // 登录成功
            map.put("res", "1");
            map.put("account_id", account.getAccountId());
            map.put("account_role", account.getAccountRole().toString());
            map.put("username", account.getUserName());
        } else {
            // 登录不成功
            map.put("res", "-2");
        }
        return map;
    }
}
